package org.geektimes.projects.user.web.listener;

import org.eclipse.microprofile.config.Config;
import org.geektimes.projects.user.web.RequestConfigHolder;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

/**
 * @author karidyang
 */
@WebListener
public class ThreadServletRequestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        System.out.println("org.geektimes.projects.user.web.listener.ThreadServletRequestListener.requestInitialized");
        ServletContext servletContext = servletRequestEvent.getServletContext();
        Config config = (Config) servletContext.getAttribute("SERVLET_CONTEXT_CONFIG");
        if (config != null) {
            RequestConfigHolder.setConfig(config);
        }
    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        System.out.println("org.geektimes.projects.user.web.listener.ThreadServletRequestListener.requestDestroyed");
        RequestConfigHolder.removeConfig();
    }


}
